// 临时的Table模组
import { ProTable } from '@ant-design/pro-components'
import { useEffect } from 'react'
import { useState } from 'react'
import request from 'umi-request'
import { Table } from 'antd'
import { Button } from 'antd/lib'
import {
    SelectionTemplateEnabled,
    SelectionTemplateRecordses,
    SelectLabAuditStatus,
    SelectLabHandleType,
    SelectionDelete,
} from './SelectionTemplate'

const SelectionTemplateMapping = {
    SelectionTemplateEnabled,
    SelectionTemplateRecordses,
    SelectLabAuditStatus,
    SelectLabHandleType,
    SelectionDelete,
}

export const ProTableTemplate002 = ({
    BoxSize,
    actionRef,
    formRef,
    configure,
    title,
    ActiveEvent,
    independence,
    active,
    selectionType,
    OptionsEvent,
}) => {
    const { columns, params, hideInSearch, options, get, pageSize } = configure
    const [IndexNumber, setIndexNumber] = useState(1)
    const [SizeNumber, setSizeNumber] = useState(pageSize || 10)
    const headers = { Authorization: sessionStorage.getItem('TOKEN') }
    const [Params, setParams] = useState(params)
    ActiveEvent.useSubscription(val => setParams(e => ({ ...e, [independence]: val.expanded })))
    useEffect(() => {
        if (independence && active) setParams({ ...params, [independence]: active })
        else setParams({ ...params, [independence]: '' })
    }, [active, independence, params])

    return (
        <ProTable
            actionRef={actionRef}
            formRef={formRef}
            rowKey="uuid"
            rowSelection={
                selectionType
                    ? { selections: [Table.SELECTION_ALL, Table.SELECTION_INVERT, Table.SELECTION_NONE] }
                    : false
            }
            tableAlertRender={({ selectedRowKeys }) => {
                const Template = SelectionTemplateMapping[selectionType.type]
                return (
                    <Template
                        selectedRowKeys={selectedRowKeys}
                        selectionType={selectionType}
                        OptionsEvent={OptionsEvent}
                        actionRef={actionRef}
                    />
                )
            }}
            tableAlertOptionRender={({ onCleanSelected }) => (
                <Button
                    type="link"
                    danger
                    onClick={() => {
                        onCleanSelected()
                        OptionsEvent.emit({ type: 'closeSelection' })
                    }}
                >
                    取消操作
                </Button>
            )}
            columns={columns}
            cardBordered
            params={Params}
            request={async (params = {}) => {
                delete params.current
                params.pageIndex = IndexNumber ?? 1
                // 判断多选
                if (params.IsCheckbox) {
                    let data = {}
                    params.IsCheckbox.forEach(item => (data = { ...data, [item]: true }))
                    delete params.IsCheckbox
                    params = { ...params, ...data }
                }
                // 判断排序条件
                if (params.SearchSort) {
                    const { SearchSort } = params
                    SearchSort.forEach((item, index) => {
                        const [field, sort] = item.split('-')
                        params[field] = `${sort}@${index}`
                    })
                    delete params.SearchSort
                }

                return request(get, { params, headers }).then(data => {
                    if (data.list) return { data: data.list, total: data.total, success: true }
                    else return { data, success: true }
                })
            }}
            search={hideInSearch ? false : { optionRender: (searchConfig, formProps, dom) => [...dom.reverse()] }}
            toolbar={{ title }}
            pagination={{
                pageSize: SizeNumber,
                showSizeChanger: true,
                onChange: (e, a) => {
                    setIndexNumber(e)
                    setSizeNumber(a)
                },
            }}
            options={{
                setting: {
                    listsHeight: 400,
                },
            }}
            dateFormatter="string"
            toolBarRender={() => {
                if (typeof options === 'function') {
                    return options(active)
                } else return options
            }}
            scroll={(() => {
                let { width: x, height } = BoxSize
                let y = hideInSearch === undefined ? height - 265 : height - 170
                return { x, y }
            })()}
        />
    )
}
